package ModelLag;
import java.util.ArrayList;

/**
 * The PersonContainer class holds up to several object of the type Person.
 * 
 * @author (Lackshan, Mikkel, Nick, patrick, Simon, Yousef) 
 * @project (Tema design)
 * @version (2014.11.04)
 */
public class PersonContainer
{
    private ArrayList<Person> persons;
    private static PersonContainer instance = null;
    
    /**
     * Constructor for objects of class PersonContainer
     */
    private PersonContainer(){
        // initialise instance variables
        persons = new ArrayList<Person>();
    }
    
    /**
     * 
     * 
     */
    public static PersonContainer getInstance(){
        if(instance == null){
            instance = new PersonContainer();
        }
        return instance;
    }

    /**
     * Adds a Person object to the persons Arraylist in the PersonContainer class
     * 
     * @param p is the user input for a specific person
     */
    public void addPerson(Person p){
        persons.add(p);
    }
    
    /**
     * Finds a specified Person Object using that persons phone
     * 
     * @param p the phone of the person
     * @param returns the Person object
     */
    
    public Person getPerson(String phone){
        int i = 0;
        boolean found = false;
        Person returnPerson = null;
        while (!found && i<persons.size()){
            Person p = persons.get(i);
            if (phone.equalsIgnoreCase(p.getPhone())){
                found = true; 
                returnPerson = p;
            }
            else{
               i++;
            }       
        }      
        return returnPerson;
    }
    
    /**
     * This method is used to delete a specified person object using that persons phone
     * 
     * @param p is the user input for a specific person
     */
    public void deletePerson(Person p){
        persons.remove(p);
    }
}
